# -*- coding: utf-8 -*-
from datetime import datetime
from setting import CODE_KEY
from sql import userid_by_openid
from web.views.tools import md5hex
from web.views.base import set_cache, get_cache

class check_user(object):

    @classmethod
    def user_code(cls,cop_id,open_id):
        now_date_min = datetime.now().strftime("%Y-%m-%d-%H-%M")
        code_key = md5hex(now_date_min+cop_id+open_id+CODE_KEY)
        data = userid_by_openid(cop_id,open_id)
        if data.get('errcode') == 0:
            set_cache(code_key,data.get('user_id'),1)
            return {
                'code_key':code_key
            }

    @classmethod
    def code_to_user(cls,code):
        cache_data = get_cache(code)
        if cache_data['errcode'] == 0:
            return cache_data['data']
        return -40022













